package com.huizhonglm.diap.presenters.user;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.model.user.RcmmerInfo;
import com.huizhonglm.diap.api.model.user.RcmmerListParams;
import com.huizhonglm.diap.api.model.user.ReferrerParams;
import com.huizhonglm.diap.api.model.user.ReferrerResponse;
import com.huizhonglm.diap.core.storage.model.ReferrerInfo;
import com.huizhonglm.diap.presenters.BasePresenter;
import com.huizhonglm.diap.ui.user.iView.RcmCashBackIView;

import java.util.List;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-07 13:53
 */
public class RcmCashBackPresenter extends BasePresenter {

    private RcmCashBackIView iView;

    public RcmCashBackPresenter(RcmCashBackIView iView) {
        this.iView = iView;
    }

    public void loadData(String userId){
        RcmmerListParams params = new RcmmerListParams();
        params.userId = userId;
        API.http().post("rcmmerList", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    List<RcmmerInfo> data = JSON.parseArray(result, RcmmerInfo.class);
                    iView.onLoadDataSuccess(data);
                } else {
                    iView.onLoadDataSuccess(null);
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onLoadDataFailure();
            }
        });
    }

    public void getReferrer(String userId){
        ReferrerParams params = new ReferrerParams(API.buildUri(API.REFERRER_GET));
        params.userId = userId;
        API.http().post("getReferrer", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    ReferrerResponse resp = JSON.parseObject(result, ReferrerResponse.class);
                    if (resp != null) {
                        ReferrerInfo info = new ReferrerInfo();
                        info.setReferrerId(resp.getReferrerId());
                        info.setReferrerName(resp.getReferrerName());
                        info.setReferrerIcon(resp.getReferrerIcon());
                        iView.onGetReferrerInfo(info);
                    } else {
                        iView.onGetReferrerInfo(null);
                    }
                } else {
                    iView.onGetReferrerInfo(null);
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onGetReferrerInfo(null);
            }
        });
    }

    public void addReferrer(String userId, final String referrerId) {
        ReferrerParams params = new ReferrerParams(API.buildUri(API.REFERRER_ADD));
        params.userId = userId;
        params.referrerId = referrerId;
        API.http().post("addReferrer", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    ReferrerResponse resp = JSON.parseObject(result, ReferrerResponse.class);
                    if (resp != null) {
                        ReferrerInfo info = new ReferrerInfo();
                        info.setReferrerId(referrerId);
                        info.setReferrerName(resp.getReferrerName());
                        info.setReferrerIcon(resp.getReferrerIcon());
                        iView.onAddReferrerSuccess(info);
                    } else {
                        iView.onAddReferrerFailure();
                    }
                } else {
                    iView.onAddReferrerFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onAddReferrerFailure();
            }
        });
    }
}
